<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;

use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;

use App\Admin\Models\distributionPrecept;
use App\Admin\Models\Common;

use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;

class DistributionPreceptController extends Controller
{
	use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('分销规则')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('分销规则')
            ->description('详细')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('分销规则')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('分销规则')
            ->description('新增')
            ->body($this->form());
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new DistributionPrecept);
        $grid->dpId('ID');
        $grid->dpName('返利名称');
		$grid->dpBenefitType('返利类型')->display(function ($dpBenefitType){
			return $dpBenefitType == 'ratio' ? '比列' :'固定';
		});
        $grid->dpBenefitType('返利类型');
        $grid->dpSelfBenefit('一级返利');
        $grid->dpParentBenefit('二级返利');
        $grid->dpGrandBenefit('三级返利');
        $grid->createTime('上传时间');

		$grid->filter(function($filter){

		    // 去掉默认的id过滤器
		    $filter->disableIdFilter();

		});

		$grid->actions(function ($actions) {
		    $actions->disableDelete();
//		    $actions->disableEdit();
//		    $actions->disableView();
		});

		$grid->filter(function($filter){

		    // 去掉默认的id过滤器
		    $filter->disableIdFilter();

		    $filter->column(1/2, function ($filter) {

		    	$filter->like('dpName', '返利名称');

			});

		    $filter->column(1/2, function ($filter) {

		    	$filter->equal('dpBenefitType', '返利类型')->select(['ratio'=>'比例','fixed'=>'固定']);

			});

		});

		// 默认展开筛选框
		$grid->expandFilter();

		// 禁用行选择checkbox
		$grid->disableRowSelector();

		// 禁用导出
		$grid->disableExport();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
    	$DistributionPrecept = new DistributionPrecept();
        $show = new Show($DistributionPrecept->findOrFail($id));

        $show->dpId('ID');
        $show->dpName('返利名称');
        $show->dpBenefitType('返利类型')->using(['ratio' => '比例', 'fixed' => '固定']);
        $show->dpSelfBenefit('一级返利');
        $show->dpParentBenefit('二级返利');
        $show->dpGrandBenefit('三级返利');
		$show->panel()
			->tools(function ($tools) {
//				$tools->disableEdit();
//				$tools->disableList();
				$tools->disableDelete();
		});;
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::Form(DistributionPrecept::class, function (Form $form) {

			$form->hidden('dpId');
			$form->text('dpName', '返利名称');
			$form->select('dpBenefitType', '返利类型')->options(['ratio' => '比例', 'fixed' => '固定']);
			$form->number('dpSelfBenefit', '一级返利')->help('类型是【固定】填对应数字。类型是【比例】填写的是百分比（不加%）');
			$form->number('dpParentBenefit', '二级返利')->help('类型是【固定】填对应数字。类型是【比例】填写的是百分比（不加%）');
			$form->number('dpGrandBenefit', '三级返利')->help('类型是【固定】填对应数字。类型是【比例】填写的是百分比（不加%）');
            $form->tools(function (Form\Tools $tools) {

			    // 去掉`列表`按钮
//			    $tools->disableList();

			    // 去掉`删除`按钮
			    $tools->disableDelete();

			    // 去掉`查看`按钮
//			    $tools->disableView();

			    // 添加一个按钮, 参数可以是字符串, 或者实现了Renderable或Htmlable接口的对象实例
//			    $tools->add('<a class="btn btn-sm btn-danger"><i class="fa fa-trash"></i>&nbsp;&nbsp;delete</a>');
			});

			$form->footer(function ($footer) {

//			    // 去掉`重置`按钮
//			    $footer->disableReset();
//
//			    // 去掉`提交`按钮
//			    $footer->disableSubmit();

			    // 去掉`查看`checkbox
			    $footer->disableViewCheck();

			    // 去掉`继续编辑`checkbox
			    $footer->disableEditingCheck();

			    // 去掉`继续创建`checkbox
//			    $footer->disableCreatingCheck();

			});
        });
    }

}
